package com.logicvoid.rosiesettingsz;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class RightActivity extends Activity {

	String TAG = "RightActivity";
	
	@Override
	public void onCreate(Bundle savedInstanceState) {		
		super.onCreate(savedInstanceState);
		/*
		ShellCommand cmd = new ShellCommand();

		try {
			cmd.sh.run("sh /system/etc/RosieUtility/RightButton.sh");

		} catch (Exception e) {
			Log.e(TAG, "Exception in RightActivity", e);
		}
		
		finish();
		*/
		

		// #### Launch From Preferences Begin ####
		
		try 
		{
			// Retrive default package if nothing assigned to button
			String defaultPackage = getText(R.string.right_button_default_package).toString();
			
			// Retrieve package name from preferences
			String prefSet = Preferences.getStringPreferenceValue("rightButtonPackageName", "", getApplicationContext());
			
			//no preference set, bail out in case Rosie calls us.
			//Rosie has been modded to NOT call RightActivity when no preference set, this is an extra safeguard
			if(prefSet.equals(""))
				finish();
			
			String packageName = Preferences.getStringPreferenceValue("rightButtonPackageName", defaultPackage, getApplicationContext());
			
			PackageManager packageManager = getPackageManager();			
			Intent intent = packageManager.getLaunchIntentForPackage(packageName);		
			
			if(null != intent)
			{
			startActivity(intent);					
			}
			else
			{
				Toast.makeText(getBaseContext(),"no intent", Toast.LENGTH_LONG).show();
			}
						
		} 
	
		catch (ActivityNotFoundException e)
		{			
			Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG).show();
			Log.e(TAG, "Exception in onCreate - ActivityNotFoundException", e);
		}
		catch (Exception e) 
		{			
			Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG).show();
			Log.e(TAG, "Exception in onCreate - Exception", e);
		}
				
		
		finish();

	}

}
